<?php
/**
 * PHPSense Pagination Class
 *
 * PHP tutorials and scripts
 *
 * @package		PHPSense
 * @author		Jatinder Singh Thind
 * @copyright	Copyright (c) 2006, Jatinder Singh Thind
 * @link		http://www.phpsense.com
 */
// ------------------------------------------------------------------------
class Pagination extends db{
	var $ajax=0;
	var $php_self;
	var $rows_per_page = 10; //Number of records to display per page
	var $total_rows = 0; //Total number of rows returned by the query
	var $links_per_page = 5; //Number of links to display per page
	var $append = ""; //Paremeters to append to pagination links
	var $sql = "";
	var $debug = false;
	var $conn = false;
	var $page = 1;
	var $max_pages = 0;
	var $offset = 0;
	var $cache_time;
	var $ext="";
	function Pagination($sql, $rows_per_page = 10, $links_per_page = 5, $append = "",$time=10,$ext="",$page='') {
		$this->ext=$ext;
		$this->cache_time=$time;
		$this->sql = $sql;
		$this->rows_per_page = (int)$rows_per_page;
		if (intval($links_per_page ) > 0) {
			$this->links_per_page = (int)$links_per_page;
		} else {
			$this->links_per_page = 5;
		}
		$this->append = $append;
		$this->php_self = htmlspecialchars($_SERVER['PHP_SELF'] );
		if (isset($_GET['page'] )) {
			$this->page = intval($_GET['page'] );
		}
	}
	
	/**
	 * Executes the SQL query and initializes internal variables
	 *
	 * @access public
	 * @return resource
	 */
	 
	
	function paginate($cache,$sql2) {
		//Load data from cache
		    // 1.Load Tong So Record
			$data=$cache->load($sql2,"pagination",1);
			if($data)
			{
			//------Lay tong so dong va ket qua;
			$this->total_rows=$data;
			}
			else
			{
				// 1. Load Result du tren limit
		//Neu load ko duoc se ket noi co so du lieu va luu data vao cache========
		$this->connect();// --------Conncect database-----------
		$this->query($sql2);
		$this->total_rows =$this->num_rows();
		$this->close();
			if($this->cache_time>0){
				$cache->save($sql2,$this->total_rows,$this->cache_time,'pagination',1);
			}
		}
		//Return FALSE if no rows found
		if ($this->total_rows == 0) {
			if ($this->debug)
			return FALSE;
		}
		//Max number of pages
		$this->max_pages = ceil($this->total_rows / $this->rows_per_page );
		if ($this->links_per_page > $this->max_pages) {
			$this->links_per_page = $this->max_pages;
		}
		//Check the page value just in case someone is trying to input an aribitrary value
		if ($this->page > $this->max_pages || $this->page <= 0) {
			$this->page = 1;
		}
		//Calculate Offset
		$this->offset = $this->rows_per_page * ($this->page - 1);
		$sql=$this->sql . " limit {$this->offset},{$this->rows_per_page}";
		$data2=$cache->load($sql,'pagination',1);
		if($data2)
		{	
			return $data2;
		}
		else
		{ 
		//Begin not cache condition
		//Fetch the required result set
		$this->connect();
		$this->query($sql);
		if ($this->num_rows()==0) {
			return false;
		}
		$data3;
		$i=0;
		if($this->ext=='truyen_1'){
				while($row=$this->fetch()){
					$truyen=truyenf1_get_truyen($row['id']);
					if($truyen && $truyen['duyet']==1){
					$rc['id']=$truyen['id'];
					$rc['ten']=$truyen['ten'];
					$rc['alias']=$truyen['alias'];
					$rc['hinhanh']=$truyen['hinhanh'];
					$rc['theloai']=$truyen['theloai'];
					$rc['theloai2']=$truyen['theloai2'];
					$rc['tacgia']=$truyen['tacgia'];
					$rc['nguon']=$truyen['nguon'];
					$rc['chuongmoinhat']=$truyen['chuongmoinhat'];
					$rc['chuongmoinhat_url']=$truyen['chuongmoinhat_url'];
					$rc['chuongbatdau']=$truyen['chuongbatdau'];
					$rc['chuongbatdau_url']=$truyen['chuongbatdau_url'];
					$rc['sochuong']=$truyen['sochuong'];
					$rc['views']=$truyen['views'];
					$rc['tinhtrang']=$truyen['tinhtrang'];
					$rc['tinhtrang-text']=$truyen['tinhtrang-text'];
					$rc['time_upload']=$truyen['time_upload'];
					$rc['user_upload']=$truyen['user_upload'];
					$rc['pick']=$truyen['pick'];
					$rc['new']=$truyen['new'];
					$rc['full']=$truyen['full'];
					$rc['follows']=$truyen['follows'];
					$data3[$i++]=$rc;
					}
				}
		}else //End of ext truyen
		if($this->ext=='truyen_2'){ // GET TRUYEN BY TAG THE LOAI
				while($row=$this->fetch()){
					$truyen=truyenf1_get_truyen($row['truyen_id']);
					if($truyen){
						if($truyen['duyet']==1){
					$rc['id']=$truyen['id'];
					$rc['ten']=$truyen['ten'];
					$rc['alias']=$truyen['alias'];
					$rc['hinhanh']=$truyen['hinhanh'];
					$rc['tacgia']=$truyen['tacgia'];
					$rc['theloai']=$truyen['theloai'];
		  		  	 $rc['theloai2']=$truyen['theloai2'];
				  	 $rc['nguon']=$truyen['nguon'];
					$rc['chuongmoinhat']=$truyen['chuongmoinhat'];
					$rc['chuongmoinhat_url']=$truyen['chuongmoinhat_url'];
					$rc['chuongbatdau']=$truyen['chuongbatdau'];
					$rc['chuongbatdau_url']=$truyen['chuongbatdau_url'];
					$rc['sochuong']=$truyen['sochuong'];
					$rc['tinhtrang']=$truyen['tinhtrang'];
					$rc['tinhtrang-text']=$truyen['tinhtrang-text'];
					$rc['views']=$truyen['views'];
					$rc['time_upload']=$truyen['time_upload'];
					$rc['user_upload']=$truyen['user_upload'];
					$rc['pick']=$truyen['pick'];
					$rc['new']=$truyen['new'];
					$rc['full']=$truyen['full'];
					$rc['follows']=$truyen['follows'];
					$data3[$i++]=$rc;
						}//End duyet==1
					}else{
						
						$this->connect();
						$this->nonquery("delete from truyen_theloai_fk where truyen_id=".$row['truyen_id']);
						echo mysql_error();
						$this->close();
					}
				}
		}else //End of ext truyen
		if($this->ext=='anime_1'){
				while($row=$this->fetch()){
					$truyen=mangaf2_get_manga($row['id']);
				if($truyen){
					if($truyen['adult']==0){
					$rc['id']=$truyen['id'];
					$rc['name']=$truyen['name'];
					$rc['alias']=$truyen['alias'];
					$rc['hinhanh']=$truyen['hinhanh'];
					$rc['theloai']=$truyen['theloai'];
					$rc['theloai2']=$truyen['theloai2'];
					$rc['nguon']=$truyen['nguon'];
					$rc['pick']=$truyen['pick'];
					$rc['new']=$truyen['new'];
					$rc['tongsoep']=$truyen['tongsoep'];
					$rc['comments']=$truyen['comments'];
					$rc['views']=$truyen['views'];
					$rc['thanks']=$truyen['thanks'];
					$rc['rate_average']=$truyen['rate_average'];
					$rc['tinhtrang']=$truyen['tinhtrang'];
					$rc['ep_ngaythem']=$truyen['ep_ngaythem'];
					$rc['tinhtrang-text']=$truyen['tinhtrang-text'];
					$rc['ep_moinhat']=$truyen['ep_moinhat'];
					$rc['ep_moinhaturl']=$truyen['ep_moinhaturl'];
					$rc['upload_time']=$truyen['upload_time'];
					$rc['upload_user']=$truyen['upload_user'];
					$rc['update_time']=$truyen['update_time'];
					
					
					$data3[$i++]=$rc;
					}
				}//If(truyen)
				}
		}else 
		if($this->ext=='anime_2'){ // GET TRUYEN BY TAG THE LOAI
				while($row=$this->fetch()){
					$truyen=mangaf2_get_manga($row['anime_id']);
					if($truyen){
							if($truyen['adult']==0){
							$rc['id']=$truyen['id'];
							$rc['name']=$truyen['name'];
							$rc['alias']=$truyen['alias'];
							$rc['hinhanh']=$truyen['hinhanh'];
							$rc['theloai']=$truyen['theloai'];
							$rc['theloai2']=$truyen['theloai2'];
							$rc['nguon']=$truyen['nguon'];
							$rc['tongsoep']=$truyen['tongsoep'];
							$rc['comments']=$truyen['comments'];
							$rc['views']=$truyen['views'];
							$rc['thanks']=$truyen['thanks'];
							$rc['rate_average']=$truyen['rate_average'];
							$rc['pick']=$truyen['pick'];
							$rc['new']=$truyen['new'];
							$rc['tinhtrang']=$truyen['tinhtrang'];
							$rc['tinhtrang-text']=$truyen['tinhtrang-text'];
							$rc['ep_ngaythem']=$truyen['ep_ngaythem'];
							$rc['ep_moinhat']=$truyen['ep_moinhat'];
							$rc['ep_moinhaturl']=$truyen['ep_moinhaturl'];
							$rc['upload_time']=$truyen['upload_time'];
							$rc['upload_user']=$truyen['upload_user'];
							$rc['update_time']=$truyen['update_time'];
						
							
							$data3[$i++]=$rc;
							}
					}//If($truyen)
					}
				}else
				if($this->ext=='blog_get_user'){ // GET TRUYEN BY TAG THE LOAI
			
					$db=new db;
					$db->connect();
					while($row=$this->fetch()){
						$row['url']=blog_url_item($row['id'],$row['alias']);
						//GET USER UPLOAD INFOR
						$db->query("select name,avatar,username from users where username='".$row['upload_user']."'");
						$row['user']=$db->fetchOne();
						//Query get subcategory------
						
						if($row['subcategory']!=''){
						
							$db->query("select id,name,alias from blog where id=".$row['subcategory']);
							if($db->num_rows()>0){
									$row2=$db->fetchOne();
									$row2['url']=blog_url_subcategory($row2['id'],$row2['alias']);
									$row['subcategory_data']=$row2;
							}
						}
							
						
						$data3[$i++]=$row;
					}
					$db->close();
				
				}
				else
				if($this->ext=='blog_get_comment'){ // GET TRUYEN BY TAG THE LOAI
					$db=new db;
					$db->connect();
					while($row=$this->fetch()){
						
						//$row['url']=blog_url_item($row['id'],$row['alias']);
						$db->query("select name,avatar,username,date_registered,chuky,slogan from users where username='".$row['upload_user']."'");
						$row['user']=$db->fetchOne();
								
						$data3[$i++]=$row;
					}
					$db->close();
				
				}else //End of ext truyen//End of ext truyen
				{
					// Lay data phan trang paging binh thuong
						while($row=$this->fetch())
							{
									$data3[$i++]=$row;
							}
				}
		
		//End of lay du lieu, close ket noi
		$this->close();
				if($this->cache_time>0){
					$cache->save($sql,$data3,$this->cache_time,'pagination',1);
				}
		return $data3;
		} //end of not cache condtion
	}
	
		function paginate_nocache($sql2) {
		$this->connect();// --------Conncect database-----------
		$this->query($sql2);
		$this->total_rows =$this->num_rows();
		$this->close();
		//Return FALSE if no rows found
		if ($this->total_rows == 0) {
		if ($this->debug)
			return FALSE;
		}
		//Max number of pages
		$this->max_pages = ceil($this->total_rows / $this->rows_per_page );
		if ($this->links_per_page > $this->max_pages) {
		$this->links_per_page = $this->max_pages;
		}
		
		//Check the page value just in case someone is trying to input an aribitrary value
		if ($this->page > $this->max_pages || $this->page <= 0) {
			$this->page = 1;
		}
			//Calculate Offset
		$this->offset = $this->rows_per_page * ($this->page - 1);
		$sql=$this->sql . " LIMIT {$this->offset}, {$this->rows_per_page}";
		//Begin not cache condition
		//Fetch the required result set
		$this->connect();
		$this->query($sql);
		if ($this->num_rows()==0) {
			return false;
		}
		$data3;
		$i=0;
		while($row=$this->fetch())
		{
		$data3[$i++]=$row;
		}
		$this->close();
		return $data3;
		} //end of not cache condtion
	function renderOlderpost($tag = 'Older Post') {
		if ($this->total_rows == 0)
			return FALSE;
		if ($this->page < $this->max_pages) {
			if($this->ajax==1){
			return '<ul class="pager"> <li  class="next"><a href="' . $this->append . ($this->page + 1) .'" onclick="ajax_pagination('.($this->page + 1) .');return false" >' . $tag . ' &rarr;</a></li> </ul>';
			}else{
			return '<ul class="pager"> <li  class="next"><a href="' . $this->append . ($this->page + 1) .'">' . $tag . ' &rarr;</a></li> </ul>';	
			}
			//return ' <ul class="pager">	 <li class="next"><a href="' . $this->append . ($this->page - 1) .'">' . $tag . ' &rarr;</a></li> </ul>';
		} else {
			return false;
		}
	}
	function renderNewerpost($tag = 'Newer Post') {
		if ($this->total_rows == 0)
			return FALSE;
		if ($this->page > 1) {
			if($this->ajax==1){
				return ' <ul class="pager">	 <li class="previous"><a href="' . $this->append . ($this->page - 1) .'" onclick="ajax_pagination('.($this->page -1) .');return false" >&larr;' . $tag . '</a></li> </ul>';
			}else{
				return ' <ul class="pager">	 <li class="previous"><a href="' . $this->append . ($this->page - 1) .'">&larr;' . $tag . '</a></li> </ul>';	
			}
		} else {
			return false;
		}
	}
	 
	function renderFirst($tag = '&lsaquo;&lsaquo;') {
		if ($this->total_rows == 0)
			return FALSE;
		if ($this->page == 1) {
			return false;
		} else {
			if($this->ajax==1){
				return '<li ><a href="' . $this->append .'1'.'" onclick="ajax_pagination(1);return false">' . $tag . '</a></li>';
			}else{
				return '<li ><a href="' . $this->append .'1'.'">' . $tag . '</a></li>';
			}
		
		}
	}
	function renderLast($tag = '&rsaquo;&rsaquo;') {
		if ($this->total_rows == 0)
			return FALSE;
		if ($this->page == $this->max_pages) {
			return false;
		} else {
			if($this->ajax==1){
				return '<li  ><a href="' . $this->append.$this->max_pages .'" onclick="ajax_pagination('.$this->max_pages.');return false">' . $tag . '</a></li>';
			}else{
			return '<li  ><a href="' . $this->append.$this->max_pages .'">' . $tag . '</a></li>';
			}
		}
	}
	function renderNext($tag = '&rsaquo;') {
		if ($this->total_rows == 0)
			return FALSE;
		
		if ($this->page < $this->max_pages) {
			if($this->ajax==1){
			return '<li ><a href="' . $this->append.($this->page + 1) .'" onclick="ajax_pagination('.($this->page+1).'); return false">' . $tag . '</a></li>';
			}else{
				return '<li ><a href="' . $this->append.($this->page + 1).'">' . $tag . '</a></li>';
			}
		} else {
			return false;
		}
	}
	
	/**
	 * Display the previous link
	 *
	 * @access public
	 * @param string $tag Text string to be displayed as the link. Defaults to '<<'
	 * @return string
	 */
	function renderPrev($tag = '&lsaquo;') {
		if ($this->total_rows == 0)
			return FALSE;
		
		if ($this->page > 1) {
			if($this->ajax==1){
			return '<li ><a href="' . $this->append .($this->page - 1) .''.'" onclick="ajax_pagination('.($this->page-1).'); return false">' . $tag . '</a></li>';
			}else{
			return '<li ><a href="' . $this->append .($this->page - 1) .''.'">' . $tag . '</a></li>';
			}
		} else {
			return false;
		}
	}
	
	/**
	 * Display the page links
	 *
	 * @access public
	 * @return string
	 */
	function renderNav($prefix = '<li >', $suffix = '</li> ') {
		if ($this->total_rows == 0)
			return FALSE;
		
		$batch = ceil($this->page / $this->links_per_page );
		$end = $batch * $this->links_per_page;
		
		
		if($end > $this->page){
		//Truong hop page dau tien 
			if($end == $this->page+( $this->links_per_page-1)&&$this->page!=1){
				$end=$end-2;
				
			}
		}
		
		if ($end == $this->page) {
			//$end = $end + $this->links_per_page - 1;
		//$end = $end + ceil($this->links_per_page/2);
			$end=$end+1;
		}
		if ($end > $this->max_pages) {
			$end = $this->max_pages;
		}
		
		
		$start = $end - $this->links_per_page + 1;
		$links = '';
		
		for($i = $start; $i <= $end; $i ++) {
			if ($i == $this->page) {
				$links .=  "<li  class='active'><span>" . $i . $suffix."</span>";
			} else {
				if($this->ajax==1){
					$links .= ' ' . $prefix . '<a href="' . $this->append . $i . '" onclick="ajax_pagination('.$i.');return false">' . $i . '</a>' . $suffix . ' ';
				}else{
				$links .= ' ' . $prefix . '<a href="' . $this->append . $i . '">' . $i . '</a>' . $suffix . ' ';
				}
			}
		}
		
		return $links;
	}
	
	/**
	 * Display full pagination navigation
	 *
	 * @access public
	 * @return string
	 */
	function renderFullNav() {
	if($this->total_rows>$this->rows_per_page)
		return "<div><ul class='pagination'><li  class='disabled'><span>Page($this->max_pages)</span></li> ".$this->renderFirst() .$this->renderPrev(). $this->renderNav() . $this->renderNext().$this->renderLast()."</ul></div>";
	}
	
	function renderFullNav2() {
	if($this->total_rows>$this->rows_per_page)
		return "<div><ul class='pagination'>".$this->renderFirst() .$this->renderPrev(). $this->renderNav() . $this->renderNext().$this->renderLast()."</ul></div>";
	}
	
	function renderFullNav3() {
		if($this->total_rows>$this->rows_per_page)
			return "<div class='pull-left'>".$this->renderNewerpost('Mới Hơn').'</div><div class="pull-right">'.$this->renderOlderpost('Cũ Hơn')."</div><div class='clearfix'></div>";
	}
	/**
	 * Set debug mode
	 *
	 * @access public
	 * @param bool $debug Set to TRUE to enable debug messages
	 * @return void
	 */
	function setDebug($debug) {
		$this->debug = $debug;
	}
	
	function get_num_page(){
	return $this->max_pages;
	}
}


class Pagination2 extends Pagination{
	public function connect(){
		$this->set_truyen();
		$this->conn=mysql_connect($this->host,$this->db_user,$this->db_pass) or die ("Can't access mysql server !!");
		mysql_query("set names 'utf8'");
		mysql_select_db($this->db_name,$this->conn);
	}
}

class Pagination3 extends Pagination{
	public function connect(){
		$this->set_cv();
		$this->conn=mysql_connect($this->host,$this->db_user,$this->db_pass) or die ("Can't access mysql server !!");
		mysql_query("set names 'utf8'");
		mysql_select_db($this->db_name,$this->conn);
	}
}
?>
